package com.gym.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gym.pojo.CallBack;
import com.gym.service.CallBackService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CallBackController {

    @Resource
    CallBackService callBackService;

    @RequestMapping("/toCallBack")
    @ResponseBody
    public Map<String,Object> toCallBack(Integer page,Integer limit,Integer userId){

        Page<CallBack> callbacks = new Page<>(page,limit);
        LambdaQueryWrapper<CallBack> queryWrapper = new LambdaQueryWrapper<>();
        //根据 userId 模糊查询
        queryWrapper.like(userId != null,CallBack::getUserId,userId);
        Page<CallBack> callBackPage = callBackService.page(callbacks,queryWrapper);
        List<CallBack> callBackList = callBackPage.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","success");
        map.put("data",callBackList);
        map.put("count",callBackPage.getTotal());
        return map;
    }

    @RequestMapping("/addCallBack")
    @ResponseBody
    public Map<String,Object> addCallBack(CallBack callBack) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = callBackService.save(callBack);
        if (save) {
            map.put("save", save);
            map.put("data", "新增成功");
        } else {
            map.put("save", save);
            map.put("data", "新增失败");
        }
        return map;
    }

    @RequestMapping("/delCallBack")
    @ResponseBody
    public Map<String,Object> delCallBack(String[] callbackId) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = callBackService.removeByIds(Arrays.asList(callbackId));
        if (save) {
            map.put("save", save);
            map.put("data", "删除成功");
        } else {
            map.put("save", save);
            map.put("data", "删除失败");
        }
        return map;
    }

    @RequestMapping("/updateCallBack")
    @ResponseBody
    public Map<String,Object> updateCourse(CallBack callBack) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = callBackService.updateById(callBack);
        if (save) {
            map.put("save", save);
            map.put("data", "信息更新成功");
        } else {
            map.put("save", save);
            map.put("data", "信息更新失败");
        }
        return map;
    }

}
